#include "TreeInfoGUI.h"
#include <limits>
using namespace std;

TreeInfoGUI::TreeInfoGUI(QWidget * parent) : QGroupBox(parent)
{
	ui.setupUi(this);
	this->setEnabled(false);

	connect(ui.treeIDBox, SIGNAL(currentIndexChanged(int)), this, SLOT(selectedTreeChanged()));
	connect(ui.speciesSpinBox,   SIGNAL(valueChanged(int)), this, SLOT(currentTreeDataModified()));
	connect(ui.modelIDSpinBox, 	 SIGNAL(valueChanged(int)), this, SLOT(currentTreeDataModified()));
	connect(ui.positionXSpinBox, SIGNAL(valueChanged(double)), this, SLOT(currentTreeDataModified()));
	connect(ui.positionYSpinBox, SIGNAL(valueChanged(double)), this, SLOT(currentTreeDataModified()));
	connect(ui.positionZSpinBox, SIGNAL(valueChanged(double)), this, SLOT(currentTreeDataModified()));
	connect(ui.rotationSpinBox,  SIGNAL(valueChanged(double)), this, SLOT(currentTreeDataModified()));
	connect(ui.areaSpinBox, 	 SIGNAL(valueChanged(double)), this, SLOT(currentTreeDataModified()));
	connect(ui.diameterSpinBox,  SIGNAL(valueChanged(double)), this, SLOT(currentTreeDataModified()));
	connect(ui.aliveCheckBox, 	SIGNAL(stateChanged(int)), this, SLOT(currentTreeDataModified()));
}
void TreeInfoGUI::selectedTree(int tree)
{
	for(int i=0;i<ui.treeIDBox->count();++i)
		if(ui.treeIDBox->itemText(i) == QString("").setNum(tree))
		{
			ui.treeIDBox->setCurrentIndex(i);
			selectedTreeChanged();
			return;
		}
}

void TreeInfoGUI::setForestState(slotsdata::ForestState state)
{
	m_currentForest = state;
	while(ui.treeIDBox->count()!=0)
		ui.treeIDBox->removeItem(0);
	for(int i=0;i<state.numberOfTrees();++i)
		ui.treeIDBox->addItem(QString("").setNum(state.at(i).treeID()));

	this->setEnabled(true);
	emit forestChanged(m_currentForest);
	
}

void TreeInfoGUI::currentTreeDataModified()
{
	dataengine::VorestTreeInfo info;

	info.m_treeID = ui.treeIDBox->currentText().toInt();
	info.m_position.x = ui.positionXSpinBox->value();
	info.m_position.y = ui.positionYSpinBox->value();
	info.m_position.z = ui.positionZSpinBox->value();
	info.m_treeDiameter = ui.diameterSpinBox->value();
	info.m_length;
	info.m_baseColor;
	info.m_rotation = ui.rotationSpinBox->value();
	info.m_area = ui.areaSpinBox->value();
	info.m_speciesID = ui.speciesSpinBox->value();
	info.m_treeHeight;
	info.m_globalHeight;
	info.m_isAlive = ui.aliveCheckBox->isChecked();
	info.m_modelID = ui.modelIDSpinBox->value();

	m_currentForest.replaceTreeInfo(info.m_treeID,info);
	emit forestChanged(m_currentForest);
}

void TreeInfoGUI::selectedTreeChanged()
{
	int currentTreeIndex = ui.treeIDBox->currentText().toInt();
	blockSignals(true);
	const dataengine::VorestTreeInfo * currentTree = NULL;

	for(int i=0;i<m_currentForest.numberOfTrees();++i)
	{
		if(m_currentForest.at(i).treeID() == currentTreeIndex)
			currentTree = &m_currentForest.at(i);
	}

	if(currentTree == NULL)
	{
		this->setEnabled(false);
		return;
	}

	ui.speciesSpinBox->setValue(currentTree->speciesID());
	ui.positionXSpinBox->setValue(currentTree->position().x);
	ui.positionYSpinBox->setValue(currentTree->position().y);
	ui.positionZSpinBox->setValue(currentTree->position().z);
	ui.rotationSpinBox->setValue(currentTree->rotation());
	ui.areaSpinBox->setValue(currentTree->area());
	ui.aliveCheckBox->setChecked(currentTree->isAlive());
	ui.diameterSpinBox->setValue(currentTree->diameter());
	ui.modelIDSpinBox->setValue(currentTree->modelID());

	this->setEnabled(true);

	blockSignals(false);
}

void TreeInfoGUI::blockSignals(bool state)
{
	ui.treeIDBox->blockSignals(state);
	ui.speciesSpinBox->blockSignals(state);
	ui.positionXSpinBox->blockSignals(state);
	ui.positionYSpinBox->blockSignals(state);
	ui.positionZSpinBox->blockSignals(state);
	ui.rotationSpinBox->blockSignals(state);
	ui.areaSpinBox->blockSignals(state);
	ui.diameterSpinBox->blockSignals(state);
	ui.aliveCheckBox->blockSignals(state);	
}


